package cn.xhy.module.purchase.controller.admin.documentserial;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.xhy.module.purchase.controller.admin.documentserial.vo.*;
import cn.xhy.module.purchase.dal.dataobject.documentserial.DocumentSerialDO;
import cn.xhy.module.purchase.service.documentserial.DocumentSerialService;
@Tag(name = "管理后台 - 单据序号")
@RestController
@RequestMapping("/purchase/document-serial")
@Validated
public class DocumentSerialController {

    @Resource
    private DocumentSerialService documentSerialService;

    @PostMapping("/create")
    @Operation(summary = "创建单据序号")
//    @PreAuthorize("@ss.hasPermission('purchase:document-serial:create')")
    public CommonResult<String> createDocumentSerial(@Valid @RequestBody DocumentSerialSaveReqVO createReqVO) {
        return success(documentSerialService.createDocumentSerial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单据序号")
    @PreAuthorize("@ss.hasPermission('purchase:document-serial:update')")
    public CommonResult<Boolean> updateDocumentSerial(@Valid @RequestBody DocumentSerialSaveReqVO updateReqVO) {
        documentSerialService.updateDocumentSerial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单据序号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('purchase:document-serial:delete')")
    public CommonResult<Boolean> deleteDocumentSerial(@RequestParam("id") Long id) {
        documentSerialService.deleteDocumentSerial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单据序号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('purchase:document-serial:query')")
    public CommonResult<DocumentSerialRespVO> getDocumentSerial(@RequestParam("id") Long id) {
        DocumentSerialDO documentSerial = documentSerialService.getDocumentSerial(id);
        return success(BeanUtils.toBean(documentSerial, DocumentSerialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单据序号分页")
    @PreAuthorize("@ss.hasPermission('purchase:document-serial:query')")
    public CommonResult<PageResult<DocumentSerialRespVO>> getDocumentSerialPage(@Valid DocumentSerialPageReqVO pageReqVO) {
        PageResult<DocumentSerialDO> pageResult = documentSerialService.getDocumentSerialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DocumentSerialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单据序号 Excel")
    @PreAuthorize("@ss.hasPermission('purchase:document-serial:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDocumentSerialExcel(@Valid DocumentSerialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DocumentSerialDO> list = documentSerialService.getDocumentSerialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单据序号.xls", "数据", DocumentSerialRespVO.class,
                        BeanUtils.toBean(list, DocumentSerialRespVO.class));
    }

}
